package cn.jtfadmin.base.data.jpa.service;

import cn.jtfadmin.base.lang.common.domain.dto.BaseExcelModalDTO;
import cn.jtfadmin.base.lang.common.domain.dto.BaseTreeExcelModalDTO;
import cn.jtfadmin.base.lang.common.domain.dto.EditPDTO;
import cn.jtfadmin.base.lang.common.domain.entity.TreeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.List;
import java.util.Objects;

/**
 * 缺省的jpa tree service
 * @author jtf
 * @since 0.0.2
 * @param <T>
 */
public class DefaultJpaTreeService< T extends TreeEntity> extends JpaTreeService<T>  implements ResourceLoaderAware {


    @Autowired
    protected JpaRepositoryImplementation<T, String> jpaRepository;


    protected ResourceLoader resourceLoader;





    @Override
    protected JpaRepositoryImplementation<T, String> getJpaRepository() {
        return jpaRepository;
    }


    @Override
    public Class<?> getAddClass() {
        Class<T> entityClass = getEntityClass();
        String simpleName = entityClass.getSimpleName();
        if(simpleName.endsWith("Entity")){
            simpleName = simpleName.substring(0, simpleName.length()-"Entity".length());
        }
        String packName = entityClass.getPackage().getName();
        if(packName.endsWith(".entity")){
            packName = packName.substring(0, packName.length()-"entity".length())+"domain";
        }
        String name = packName+"."+simpleName+"AddPDTO";
        try {
            return resourceLoader.getClassLoader().loadClass(name);
        } catch (ClassNotFoundException e) {
            return super.getAddClass();
        }
    }


    @Override
    public Class<? extends EditPDTO> getEditClass() {
        Class<T> entityClass = getEntityClass();
        String simpleName = entityClass.getSimpleName();
        if(simpleName.endsWith("Entity")){
            simpleName = simpleName.substring(0, simpleName.length()-"Entity".length());
        }
        String packName = entityClass.getPackage().getName();
        if(packName.endsWith(".entity")){
            packName = packName.substring(0, packName.length()-"entity".length())+"domain";
        }
        String name = packName+"."+simpleName+"EditPDTO";
        try {
            return (Class<? extends EditPDTO>) resourceLoader.getClassLoader().loadClass(name);
        } catch (ClassNotFoundException e) {
            return super.getEditClass();
        }
    }

    @Override
    public Class<? extends BaseTreeExcelModalDTO> getExcelClass() {
        Class<T> entityClass = getEntityClass();
        String simpleName = entityClass.getSimpleName();
        if(simpleName.endsWith("Entity")){
            simpleName = simpleName.substring(0, simpleName.length()-"Entity".length());
        }
        String packName = entityClass.getPackage().getName();
        if(packName.endsWith(".entity")){
            packName = packName.substring(0, packName.length()-"entity".length())+"domain";
        }
        String name = packName+"."+simpleName+"ExcelDTO";
        try {
            return (Class<? extends BaseTreeExcelModalDTO>) resourceLoader.getClassLoader().loadClass(name);
        } catch (ClassNotFoundException e) {
            return super.getExcelClass();
        }
    }


    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}